<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root"></div>
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script type="text/babel">
        class InputElement extends React.Component{
            constructor(props){
                super(props);
                this.state = {
                    info:""
                }
            }
            
            render(){
                return <div>
                        <label>{this.props.children}</label>
                        <input 
                        type={this.props.type} 
                        onBlur={this.props.validate} 
                        onInput={this.props.onInput}/>
                        <span>{this.props.info}</span>
                    </div>
            }
        }
        class Reg extends React.Component{
            constructor(props){
                super(props);
                this.state = {
                    username:{
                        valid:false,
                        info:""
                    },
                    pwd:{
                        value:"1"
                    }
                }
                
            }
            handleClick = () => {
             
            }
            validateUsername = (e) => {
                if(!/^\w{6,20}$/.test(e.target.value)){
                    this.setState({
                        username:{
                            valid:false,
                            info:"格式不正确",
                            // value:e.target.value
                        }
                    });
                }else{
                    this.setState({
                        username:{
                            valid:true,
                            info:"√",
                            // value:e.target.value
                        }
                    });
                }
            }
            handleSubmit = () => {
                console.log(this.state.username.value)
            }
            render(){
                return <div>
                        <InputElement type="text"
                         info={this.state.username.info}
                         onInput={(e) => this.setState({username:{value:e.target.value}})} >用户名：</InputElement>
                        <InputElement type="password" value={this.state.pwd.value} >密码：</InputElement>
                        <InputElement type="password" >确认密码：</InputElement>
                        <button type="button" onClick={this.handleSubmit}>确定</button>
                    </div>
            }
        }

        ReactDOM.createRoot(document.getElementById("root")).render(<Reg />);
    </script>
</body>
</html>